import os
import time

import yaml

from automation_api.conftest import root_path


class Until:
    @classmethod
    def git_file_path(cls, path_name):
        """
        获取绝对路径
        :return:
        """
        path = os.sep.join([root_path, path_name])
        return path

    @classmethod
    def git_yaml_path(cls, yaml_path):
        """
        获取yaml的绝对路径
        :param yaml_path:
        :return:
        """
        with open(yaml_path, 'r', encoding='utf-8') as f:
            date = yaml.safe_load(f)
            return date

    @classmethod
    def git_current_time(cls):
        """
        时间戳
        :return:
        """
        return time.strftime('%Y-%m-%d-%H-%M-%S')

    @classmethod
    def save_source_dates(cls, source_type):
        """
        保存文件
        :param source_type:
        :return:
        """
        if source_type == 'images':
            end = '.png'
            _path = 'images'
        elif source_type == 'pagesource':
            end = '.xml'
            _path = 'pagesource'
        else:
            return None
        source_name = Until.git_current_time() + end
        source_dir_path = os.sep.join([root_path, _path])
        if not os.path.isdir(source_dir_path):
            os.mkdir(source_dir_path)
        source_file_path = os.sep.join([source_dir_path, source_name])
        return source_file_path
